package com.bst.bsj.bops.web.biz.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bst.bsj.bops.web.biz.SeedlingTypeService;
import com.bst.bsj.common.dal.daointerface.SeedlingTypeDAO;
import com.bst.bsj.common.dal.dataobject.SeedlingType;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.ResultSupport;

@Service("seedlingTypeService")
public class SeedlingTypeServiceImpl implements SeedlingTypeService {

  private static final Log logger = LogFactory.getLog(SeedlingTypeServiceImpl.class);

  @Autowired
  private SeedlingTypeDAO seedlingTypeDAO;

  @Override
  public Result query(Map paramMap) {
    Result result = new ResultSupport(false);

    Long offset = (Long) paramMap.get("offset");
    Long length = (Long) paramMap.get("length");
    Integer page = (Integer) paramMap.get("page");
    Long total = 0L;
    List<SeedlingType> list = seedlingTypeDAO.getCounts(paramMap);
    total = (long) list.size();
    List<SeedlingType> seedlingTypes = seedlingTypeDAO.selectAll(paramMap, offset, length);

    // 创建一个分页器(leng.intValue：每页多少条)
    Paginator paginator = new Paginator(length.intValue());
    // 设置总项数
    paginator.setItems(total == null ? 0 : total.intValue());
    // 设置当前页
    paginator.setPage(page);
    result.getModels().put("paginator", paginator);
    result.getModels().put("seedlingTypes", seedlingTypes);

    result.setSuccess(true);
    return result;
  }

  @Override
  public SeedlingType getSeedlingType(Long id) {
    return seedlingTypeDAO.getSeedlingType(id);
  }

  @Override
  public int update(SeedlingType seedlingType) {
    int lid = 0;
    try {

      lid = seedlingTypeDAO.update(seedlingType);
    } catch (Exception e) {
      logger.error("SeedlingTypeServiceImpl.update(banner) error!", e);
    }
    return lid;
  }

  @Override
  public Long insert(SeedlingType seedlingType) {
    Long lid = 0L;
    try {
      lid = seedlingTypeDAO.insert(seedlingType);
      SeedlingType type = new SeedlingType();
      type.setId(lid);
      type.setNumber(lid + "");
      seedlingTypeDAO.update(type);
    } catch (Exception e) {
      logger.error("SeedlingTypeServiceImpl.insert(banner) error!", e);
    }
    return lid;
  }

  @Override
  public int delete(Long id) {
    int lid = 0;
    try {
      lid = seedlingTypeDAO.delete(id);
    } catch (Exception e) {
      logger.error("SeedlingTypeServiceImpl.delete(banner) error!", e);
    }
    return lid;
  }

  @Override
  public List<SeedlingType> getAll() {
    return seedlingTypeDAO.getAll();
  }

  @Override
  public SeedlingType getByName(String name) {
    return seedlingTypeDAO.getByName(name);
  }

  @Override
  public SeedlingType getCountByName(String name) {
    return seedlingTypeDAO.getCountByName(name);
  }

}
